1. Field of the Invention
The invention relates to templates and, more particularly, the invention relates to template instantiation for cross compilers.
2. Background of the Related Art
Computer programs are conventionally written in a high-level language, such as C++. C++ is an object-oriented programming language in which programs are created using abstractions and constructs to create user-defined classes for defining the methods and variables for a particular type of object. All objects of a particular class are identical in form and behavior but contain different data on their variables.
The text of a computer program written in such a high-level language is called the source code. However, to more efficiently run a computer program, the computer program is conventionally converted from source code to machine language. A compiler is a computer program that converts or, more particularly, compiles source code into machine language. A compiled version of source code is called object code.
Many computer programs are written in modules, especially programs written by a team of developers. Accordingly, source code modules, after compilation, form object code modules. One or more of these modules may have one or more associated templates. Templates or parameterized types conventionally tell a compiler how to generate code at compile time. Templates may be likened to macros, as templates may be used to implement data structures or algorithms. The C++ language also provides templates, which define families of classes and functions that are structurally identical but contain different kinds of data in their variables or function parameters. A class template specifies how individual classes can be constructed. A function template specifies how individual functions can be constructed. Each family of classes and functions is specified by a template declaration, and each member of the family is called an instantiation. Instantiations are created by the compiler. During compilation, a compiler will track which templates are needed for a set of modules. The compiler will generate template code for those tracked templates. It should be understood that some templates depend upon other templates, which may depend upon still other templates, and so forth. Thus, instantiated templates may have one or more other template dependencies.
Object code modules are “linked” together by a computer program called a linker, to form an executable program. During “link” time, namely the time during which a group of modules is “linked” together, template object code is combined with other object code to provide template information associations for the executable program.
A template can have multiple template arguments. When a template is used, the actual types for the template arguments must be known. The compiler automatically generates implementations of the template by substituting the actual types for the template argument types. This is referred to as template instantiation. By automatic template instantiation, it is meant that a programmed computer is capable of extracting one or more templates from source code and creating template source code without human intervention. When the compiler detects that a template is being used, it instantiates a version of the template with the specified type arguments by making a copy of the definition of each required function with the actual template arguments substituted for the formal template arguments. The copy is compiled to produce object code that can then be passed to the linking step. To provide template instantiations, a compiler and a linker work together to ensure each template instantiation occurs only one time if it is needed in the executable program, and does not occur at all if it is not needed.
Advantageously, automatic instantiation of templates facilitates creation of an executable application without having to manually track template information and without having to manually instantiate templates.
Conventionally, a compiler compiles source code for operation on the same operating system platform that the compiler operates. For example, a compiler running on an OS/2 platform would compile for operation on that platform. However, a cross compiler generates target object code for a platform different from the one on which it operates. For example, a cross compiler may run on an IBM Think Pad with an OS/2 platform, but generate object code for execution on an IBM AS/400 computer running on an OS/400 platform. After generation of cross-compiled object code, the cross-compiled object code is moved to the target platform. A linker on the target platform links the object code to provide an executable program.
A C++ cross compiler from others works only on platforms with built-in linker support to collapse template instances. In other words, for cross-platform operation between different kinds of platforms a specialized linker designed to operate on the target platform must be used as opposed to a common platform linker. Moreover, because this C++ cross compiler uses the code equivalent of common blocks, compilation time is increased owing to repeated compiling of template code and duplicate templates.
A better approach to automatic template instantiation is creation of a template repository, namely an automatically maintained location where template instances are stored. In this approach, as individual object code files are built, a compiler places any template definitions and instantiations encountered into the repository. A link wrapper, which works with a common platform linker, adds in objects in the repository and compiles any needed instances not previously generated. Though this approach provides reduced compilation time over a common block approach, uses a standard system linker as opposed to a platform specific linker, and is more scalable over duplicate template generation, heretofore there has been no cross compiler solution implementing a template repository solution. By standard linker, it is meant a linker without template instantiation capability.
Therefore, a need exists in the art for a cross compiler with automatic template instantiation employing a template repository.